package com.missilike.api.controller;

import com.missilike.biz.*;
import com.missilike.core.domain.AccountInfoRequest;
import com.missilike.core.domain.DeleteContentReuquest;
import com.missilike.core.domain.RewardAccountRequest;
import com.missilike.core.result.BaseResultCode;
import com.missilike.core.result.CommonResult;
import com.missilike.core.service.IIMService;
import com.missilike.core.utils.ResultUtils;
import com.missilike.domain.response.MilAccountInfo;
import com.missilike.domain.response.ThirdIdResponse;
import com.missilike.repository.entity.MilAccount;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/accountInfo")
public class AccountInfoController {
    private final IMilAccountService accountService;
    private final IMilContentAppreciateService appreciateService;
    private final IMilAccountContentService accountContentService;
    private final IMilRewardRecordService rewardRecordService;
    private final IMilContentCollectionService contentCollectionService;
    private final IMilFollowRelationshipService followRelationshipService;
    private final IIMService iimService;
    private final IMilAccountThirdService iMilAccountThirdService;

    public AccountInfoController(IMilAccountService accountService, IMilFollowRelationshipService followRelationshipService, IMilContentCollectionService contentCollectionService, IMilContentAppreciateService appreciateService, IMilAccountContentService accountContentService, IMilRewardRecordService rewardRecordService,IIMService iimService,IMilAccountThirdService iMilAccountThirdService) {
        this.accountService = accountService;
        this.appreciateService = appreciateService;
        this.accountContentService = accountContentService;
        this.rewardRecordService = rewardRecordService;
        this.contentCollectionService = contentCollectionService;
        this.followRelationshipService = followRelationshipService;
        this.iMilAccountThirdService=iMilAccountThirdService;
        this.iimService=iimService;
    }

    @ApiOperation(value = "updateAccountInfo", notes = "修改用户信息")
    @RequestMapping(value = "/updateAccountInfo", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public CommonResult update(@RequestBody @ApiParam(value = "性别1男2女")
                                       AccountInfoRequest milAccount) {
        MilAccount account = accountService.updateAccountInfo(milAccount);
        return ResultUtils.ok(account);
    }

    @ApiOperation(value = "getUserInfo", notes = "获取用户信息")
    @RequestMapping(value = "/getUserInfo", method = RequestMethod.GET, produces = "application/json")
    public CommonResult getUserInfo(@RequestParam String accountId) {
        if (StringUtils.isBlank(accountId)) {
            return ResultUtils.error(BaseResultCode.PARAMETER_ERROR.getCode(), "入参为null");
        }
        MilAccountInfo info = accountService.getAccount(accountId);
        info.setBeFollowed(followRelationshipService.getBeFollowedCount(accountId));
        info.setBeCollection(contentCollectionService.getUserBeCollectionCount(accountId));
        info.setCollection(contentCollectionService.getUserCollectionCount(accountId));
        info.setContented(accountContentService.getAccountContentCount(accountId));
        return ResultUtils.ok(info);
    }

    @ApiOperation(value = "getThirdId", notes = "获取用户绑定的第三方账号")
    @RequestMapping(value = "/getThirdId", method = RequestMethod.GET, produces = "application/json")
    public CommonResult getThirdId(String thirdId) {
        MilAccountInfo milAccountInfo=iMilAccountThirdService.getThirdId(thirdId);
        String  imSign = iimService.getUserSign(milAccountInfo.getAccount());
        milAccountInfo.setBeFollowed(followRelationshipService.getBeFollowedCount(milAccountInfo.getId()));
        milAccountInfo.setBeCollection(contentCollectionService.getUserBeCollectionCount(milAccountInfo.getId()));
        milAccountInfo.setCollection(contentCollectionService.getUserCollectionCount(milAccountInfo.getId()));
        milAccountInfo.setContented(accountContentService.getAccountContentCount(milAccountInfo.getId()));
        return ResultUtils.ok(new ThirdIdResponse(milAccountInfo,imSign));
    }

    @ApiOperation(value = "addEstablishThirdAccount", notes = "绑定第三方账户  1微信 2微博 3QQ")
    @RequestMapping(value = "/addEstablishThirdAccount", method = RequestMethod.GET, produces = "application/json")
    public CommonResult addEstablishThirdAccount(@RequestParam String thirdId,@RequestParam String accountId,@RequestParam Integer type) {
        iMilAccountThirdService.addEstablishThirdAccount(thirdId,accountId,type);
        return ResultUtils.ok();
    }

    @ApiOperation(value = "removeEstablishThirdAccount", notes = "删除第三方账户绑定  1微信 2微博 3QQ")
    @RequestMapping(value = "/removeEstablishThirdAccount", method = RequestMethod.GET, produces = "application/json")
    public CommonResult removeEstablishThirdAccount(@RequestParam String thirdId,@RequestParam String accountId,@RequestParam Integer type) {
        iMilAccountThirdService.removeEstablishThirdAccount(thirdId,accountId,type);
        return ResultUtils.ok();
    }



    //todo   以及好友申请列表  获取消息列表

    //todo  评论朋友圈


    //todo 点赞 收藏
    @ApiOperation(value = "rewardAccounts", notes = "点赞/打赏")
    @RequestMapping(value = "/rewardAccounts", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public CommonResult rewardAccounts(@RequestBody RewardAccountRequest request) {
        appreciateService.rewardAccounts(request);
        return ResultUtils.ok();
    }

    @ApiOperation(value = "deleteContent", notes = "删除长文或者图文")
    @RequestMapping(value = "/deleteContent", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public CommonResult deleteContent(@RequestBody DeleteContentReuquest reuquest) {
        if (accountContentService.deleteContent(reuquest))
            return ResultUtils.ok();
        else
            return ResultUtils.error();
    }


    @ApiOperation(value = "getRewardDetail", notes = "获取打赏详细")
    @RequestMapping(value = "/getRewardDetail", method = RequestMethod.GET, produces = "application/json")
    public CommonResult getRewardDetail(@RequestParam String accountId) {
        if (StringUtils.isBlank(accountId)) {
            return ResultUtils.error(99901, "参数不能为空");
        }
        return ResultUtils.ok(rewardRecordService.getRewardDetail(accountId));
    }

    @ApiOperation(value = "getAccountsBrowseCount", notes = "获取浏览历史")
    @RequestMapping(value = "/getAccountsBrowseCount", method = RequestMethod.GET, produces = "application/json")
    public CommonResult getAccountsBrowseCount(@RequestParam String accountId) {
        return ResultUtils.ok(accountService.getAccountsFriendInfo(accountId));
    }
}
